package com.lib.base.base

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.lib.base.R
import com.lib.base.utils.ActivityUtils
import com.lib.base.utils.ToastUtils
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_base.*

/**
 *
 */
abstract class BaseActivity : AppCompatActivity(), BaseView, View.OnClickListener {

    /**  布局id---2019-06-13 */
    abstract val layoutId: Int?

    override var mContext: Context? = null

    /** 默认layout ---2019-06-13 */
    override val defLayoutId: Int = R.layout.activity_default

    /**视图重载是否重新加载数据*/
    abstract val reload: Boolean

    /**  Rx方式进行动态权限请求---2019-06-13 */
    var mRxPermissions: RxPermissions? = null


    /**  状态栏颜色---2019-06-13 */
    abstract var statusBarColor: Int?


    /**基础交互页面实体*/
    /**网络错误页面*/
    private var baseNetErrorView: View? = null
    /**数据加载中界面*/
    private var baseDataLoadingView: View? = null
    /**请求数据为空界面*/
    private var baseDataNullView: View? = null
    /**服务器错误界面*/
    private var baseNet404View: View? = null

    /** 初始化数据 ---2019-06-13 */
    abstract fun initData(savedInstanceState: Bundle?)


    /** 初始化布局 ---2019-06-13 */
    abstract fun initView()

    /**获取数据*/
    abstract fun getData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        mContext = this

        mRxPermissions = RxPermissions(this)

        ActivityUtils.instance.addActivity(this)
        initData(savedInstanceState)

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

        if (null != layoutId) {
            setContentView(layoutId!!)
            initView()
        } else {
            setContentView(defLayoutId)
        }
        /**修改状态栏颜色，默认主题色*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.statusBarColor = statusBarColor
                ?: ContextCompat.getColor(this, R.color.colorPrimary)
        }

        // 统一处理没有网络或网络错误的点击效果，一般为重新获取数据 getData()
        ll_base?.setOnClickListener(this)

    }

    override fun showToast(msg: String?) {
        ToastUtils.showToast(mContext, msg ?: "无消息")
    }

    override fun finishThis() {
    }

    override fun changeBaseView(state: BaseView.STATE) {
        ll_base?.removeAllViews()
        ll_base?.isClickable = false

        val baseView = when (state) {
            BaseView.STATE.NET_ERROR -> {
                baseNetErrorView
                    ?: LayoutInflater.from(mContext).inflate(R.layout.activity_base_net_error, ll_base, false)
            }
            BaseView.STATE.SERVER_ERROR -> {
                baseNet404View
                    ?: LayoutInflater.from(mContext).inflate(R.layout.activity_base_404, ll_base, false)
            }
            BaseView.STATE.NOT_FOUND -> {
                baseNet404View
                    ?: LayoutInflater.from(mContext).inflate(R.layout.activity_base_404, ll_base, false)
            }
            BaseView.STATE.LOADING -> {
                baseDataLoadingView
                    ?: LayoutInflater.from(mContext).inflate(R.layout.activity_base_loading, ll_base, false)
            }
            BaseView.STATE.RESULT_NULL -> {
                baseDataNullView
                    ?: LayoutInflater.from(mContext).inflate(R.layout.activity_base_null, ll_base, false)
            }
            BaseView.STATE.OK -> {
                null
            }
        }

        if (baseView == null) {
            ll_base?.visibility = View.GONE
        } else {
            ll_base?.visibility = View.VISIBLE
            ll_base?.addView(baseView)
        }

    }

    override fun setBaseView(
        baseNetErrorView: View?,
        baseDataLoadingView: View?,
        baseDataNullView: View?,
        baseNet404View: View?
    ) {
        this.baseNetErrorView = baseNetErrorView
        this.baseDataLoadingView = baseDataLoadingView
        this.baseDataNullView = baseDataNullView
        this.baseNet404View = baseNet404View
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_base -> {
                if (ll_base.isVisible) {
                    // 没有网络或网络错误点击是重新获取数据
                    changeBaseView(BaseView.STATE.LOADING)
                    getData()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (reload) getData()
    }

}